package Demo06.Demo05;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class Demo06_Test3 {
    public static void main(String[] args) {
        File file=new File("G:\\myFiles");
        String[] strings=file.list();
 
        File file2=new File("G:\\myFiles\\txt");
        if(!file2.exists()){
            file2.mkdirs();
        }   
 
        for (String string : strings) {
            if(string.endsWith(".txt")){
                //获取文件名
                String string2=string.substring(0, 5);
                //重命名
                File file3=new File(file,string);
                file3.renameTo(new File(file,string2.toUpperCase()+".txt"));
                //复制新文件到txt文件夹下
                FileInputStream fis=null;
                FileOutputStream fos=null;
                try {
                    fis=new FileInputStream(file3);
                    fos=new FileOutputStream(new File(file2,string2.toUpperCase()+".txt"));
                    byte[] bs=new byte[50];
                    int count=0;
                    while((count=fis.read(bs))!=-1){
                        fos.write(bs, 0, count);
                        fos.flush();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fis!=null){
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(fos!=null){
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
